<?php
namespace App\Models;
use Illuminate\Http\Request;
use DB;



/**
 * 管理员模型类
 */
class Project_admin_user extends BaseModel
{

    public $table = "project_admin_user";    //指定表名


    /**
     * @param 检测用户名是否存在
     * @return bool
     */
    public  static function existUserName($request)
    {
        $admin = Project_admin_user::where('username',$request->username)->first();
        return $admin;
    }

    /**
     * 获取登录用户的所有权限
     * @param $admin
     * @return mixed
     */
    public static function permission($admin)
    {
        //获取用户的所有权限
        $allPer = DB::select("select nr.username,nr.mode,nr.path from project_admin_user_role ur,project_admin_role_node rr,project_admin_node nr where ur.rid=rr.rid and rr.nid=nr.id and ur.uid=:id",['id'=>$admin->id]);
        return $allPer;
    }

    public function existAdminUsername($request)
    {
        $nameCount  = Project_admin_user::where('username',$request->username)->count();
        if($nameCount != 0){
             return true;
        } else {
            return false;
        }

    }

    /**
     * $first  1 代表编辑  0代表添加
     * 用户的添加或编辑保存
     */
    public function createOrSave($request,$first )
    {
        $this->username = $request->username;
        $this->password = $request->password;
        $this->email    = $request->email;
        if($first = 0){
            $this->lasttime = date('Y-m-d H:i:s', time());
            $this->addtime = data('Y-m-d H:i;s',time());
        } elseif ($first = 1){
            $this->lasttime = date('Y-m-d H:i:s', time());
        }
        return $this->save();
    }
}